package com.wn.icloud.domain;

import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.Instant;

/**
 * 使审计信息生效需在项目中添加相关配置
 * 参考：https://www.jianshu.com/p/14cb69646195
 *
 * @author liguangca
 * @date 2018/11/30 14:47
 */
@SuppressWarnings("serial")
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class) // 审计信息
public class BaseEntityDomain extends BaseDomain {

    /**
     * 创建时间
     */
    @Column(nullable = false)
    @CreatedDate
    protected Instant createdDate;
    /**
     * 创建人
     */
    @Column(nullable = false)
    @CreatedBy
    protected String createdBy;

    /**
     *最后修改日期
     */
    @Column(nullable = false)
    @LastModifiedDate
    protected Instant lastModifiedDate;

    /**
     * 最后修改人
     */
    @Column(nullable = false)
    @LastModifiedBy
    protected String lastModifiedBy;

    /**
     * 是否被删除 0存在 1删除
     */
    @Column(nullable = false)
    protected boolean deleted = false; // 是否被删除了

    @Override
    public String toString() {
        return null;
    }

    public BaseEntityDomain() {
    }

    public BaseEntityDomain(Instant createdDate, String createdBy, Instant lastModifiedDate, String lastModifiedBy, boolean deleted) {
        this.createdDate = createdDate;
        this.createdBy = createdBy;
        this.lastModifiedDate = lastModifiedDate;
        this.lastModifiedBy = lastModifiedBy;
        this.deleted = deleted;
    }

    public BaseEntityDomain(Long id,Instant createdDate, String createdBy, Instant lastModifiedDate, String lastModifiedBy, boolean deleted) {
        this.id= id;
        this.createdDate = createdDate;
        this.createdBy = createdBy;
        this.lastModifiedDate = lastModifiedDate;
        this.lastModifiedBy = lastModifiedBy;
        this.deleted = deleted;
    }

    @Override
    public boolean equals(Object o) {
        if (null != o) {
            if (o instanceof BaseEntityDomain) {
                BaseEntityDomain domain = (BaseEntityDomain) o;
                if (this.id != null && this.id.equals(domain.id)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        if (null == this.id) {
            this.id = 0L;
        }
        return HashCodeBuilder.reflectionHashCode(this.id);
    }

    public Instant getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Instant createdDate) {
        this.createdDate = createdDate;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Instant getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(Instant lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

}
